package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.service.MemberInfoManagementService;
import com.cskaoyan.market.db.domain.MarketUser;
import com.cskaoyan.market.db.domain.MarketUserExample;
import com.cskaoyan.market.db.mapper.MarketUserMapper;
import com.cskaoyan.market.util.MyBatisUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @author TangHonghao
 * @since 2023/03/20 15:57
 */
public class MemberInfoManagementServiceImpl implements MemberInfoManagementService {

    @Override
    public MarketUser selectMemberInfo(Integer id) {
        SqlSession sqlSession = MyBatisUtil.getSession();
        MarketUserMapper userMapper = sqlSession.getMapper(MarketUserMapper.class);
        MarketUser marketUser = userMapper.selectByPrimaryKey(id);
        sqlSession.commit();
        sqlSession.close();
        return marketUser;
    }

    @Override
    public List<MarketUser> selectMemberInfo(String username) {
        SqlSession sqlSession = MyBatisUtil.getSession();
        MarketUserMapper userMapper = sqlSession.getMapper(MarketUserMapper.class);
        MarketUserExample marketUserExample = new MarketUserExample();
        if(!StringUtils.isEmpty(username)){
            marketUserExample.or().andUsernameLike("%"+username.trim()+"%");
        }
        List<MarketUser> marketUsers = userMapper.selectByExample(marketUserExample);
        sqlSession.commit();
        sqlSession.close();
        return marketUsers;
    }

    @Override
    public List<MarketUser> selectMemberInfoAll() {
        SqlSession sqlSession = MyBatisUtil.getSession();
        MarketUserMapper userMapper = sqlSession.getMapper(MarketUserMapper.class);
        MarketUserExample marketUserExample = new MarketUserExample();
        List<MarketUser> marketUsers = userMapper.selectByExample(marketUserExample);
        sqlSession.commit();
        sqlSession.close();
        return marketUsers;
    }

    @Override
    public List<MarketUser> selectMemberInfoByMobile(String mobile) {
        SqlSession sqlSession = MyBatisUtil.getSession();
        MarketUserMapper userMapper = sqlSession.getMapper(MarketUserMapper.class);
        MarketUserExample marketUserExample = new MarketUserExample();
        marketUserExample.or().andMobileEqualTo(mobile);
        List<MarketUser> marketUsers = userMapper.selectByExample(marketUserExample);
        return marketUsers;
    }

}
